package com.zhny.controller;

import com.github.pagehelper.Page;
import com.zhny.entity.PageResult;
import com.zhny.entity.QueryPageBean;
import com.zhny.entity.Result;
import com.zhny.pojo.Menu;
import com.zhny.pojo.Role;
import com.zhny.service.MenuService;
import com.zhny.service.RoleService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/menu")
@RestController
public class MenuController {

    @Reference
    private MenuService menuService;

    //页面查询
    @GetMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        Page<Menu> pageList = menuService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());

        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());

        return pageResult;
    }

    /**
     * 根据id查找
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {

        Menu menu = menuService.findById(id);

        return new Result(true,"查询用户成功",menu);
    }

    /**
     * 添加
     * @param menu
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Menu menu) {

        menuService.add(menu);

        return new Result(true,"新添用户成功");
    }

    /**
     * 修改
     * @param menu
     * @return
     */
    @PutMapping
    public Result edit(@RequestBody Menu menu) {

        menuService.edit(menu);

        return new Result(true,"编辑用户成功");
    }

    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id) {

        menuService.delete(id);

        return new Result(true,"删除用户成功");
    }

}
